package com.saikuo.service.controller;

import com.saikuo.service.api.ApiResultModel;
import com.saikuo.service.constant.DeliveryType;
import com.saikuo.service.service.IOrderService;
import com.saikuo.service.service.IPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jdom.JDOMException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.*;

/**
 * Created by shanchangyu on 16/10/13.
 */
@Api(value = "订单相关接口", description = "订单生成，支付，查询等")
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IPayService payService;

    @ApiOperation(value = "创建订单")
    @RequestMapping(value = "/createOrder", method = RequestMethod.POST)
    public ApiResultModel createOrder(@RequestParam int userId,
                                      @RequestParam String name, @RequestParam String mobile, @RequestParam String address,
                                      @RequestParam String comments, @RequestParam String orderDetail, @RequestParam Integer userCouponId, @RequestParam Double price, @RequestParam Double postage, @RequestParam String storeCode, @RequestParam int payWay) {
        return orderService.createOrderInfo(userId, name, mobile, address, comments, orderDetail, userCouponId, price, postage, storeCode, payWay);
    }

    @ApiOperation(value = "修改订单状态,订单状态-1:订单取消  0:待付款 1：待发货 2：待收货 3：已完成 4:删除订单")
    @RequestMapping(value = "/updateOrder", method = RequestMethod.POST)
    public ApiResultModel updateOrder(@RequestParam Long orderId, @RequestParam int status) {
        return orderService.updateOrderInfo(orderId, status);
    }

    @ApiOperation(value = "获取用户所有订单,支持分页;page:页码:默认传递1,订单状态-1:订单取消  0:待付款 1:待发货 2:待收货 3:已完成 10:所有订单(不含已经删除)")
    @RequestMapping(value = "/getOrdersByUserId", method = RequestMethod.GET)
    public ApiResultModel getOrdersByUserId(@RequestParam int userId, @RequestParam int orderStatus, @RequestParam int page) {
        return orderService.getOrdersByUserId(userId, orderStatus, page);
    }

    @ApiOperation(value = "获取订单详情信息")
    @RequestMapping(value = "/getOrderDetailById", method = RequestMethod.GET)
    public ApiResultModel getOrderDetailById(@RequestParam Long orderId) {
        return orderService.getOrderByOrderId(orderId);
    }

    @ApiOperation(value = "用户取消未付款订单")
    @RequestMapping(value = "/cancelOrder", method = RequestMethod.GET)
    public ApiResultModel cancelOrder(@RequestParam Long orderId) {
        return orderService.cancelOrder(orderId);
    }

    @ApiOperation(value = "订单发起投诉")
    @RequestMapping(value = "/complainOrder", method = RequestMethod.GET)
    public ApiResultModel complainOrder(@RequestParam Long orderId, @RequestParam int userId, @RequestParam String phone, @RequestParam String content) {
        return orderService.complainOrder(orderId, userId, phone, content);
    }

    @ApiOperation(value = "订单支付(统一下单),payType支付方式1=微信;2=支付宝")
    @RequestMapping(value = "/payOrder", method = RequestMethod.GET)
    public ApiResultModel payOrder(@RequestParam Long orderId, @RequestParam int payType, HttpServletRequest request) {
        return payService.payOrder(orderId, payType, request);
    }


    @ApiOperation(value = "微信支付自动回调接口")
    @RequestMapping(value = "/weChatNotifyUrl", method = RequestMethod.POST)
    @ResponseBody
    public void weChatNotifyUrl(ServletRequest request, ServletResponse response)
            throws IOException, JDOMException {
        payService.weChatNotifyUrl(request, response);
    }

    @ApiOperation(value = "支付宝支付自动回调接口-小账户负责邮寄")
    @RequestMapping(value = "/sAliPayNotifyUrl", method = RequestMethod.POST)
    @ResponseBody
    public void aliPayNotifyUrl(ServletRequest request, ServletResponse response) throws IOException {
        payService.aliPayNotifyUrl(request, response, DeliveryType.MAIL.getValue());
    }

    @ApiOperation(value = "支付宝支付自动回调接口-大账户负责送药到门")
    @RequestMapping(value = "/lAliPayNotifyUrl", method = RequestMethod.POST)
    @ResponseBody
    public void lAliPayNotifyUrl(ServletRequest request, ServletResponse response) throws IOException {
        payService.aliPayNotifyUrl(request, response, DeliveryType.DOOR.getValue());
    }

}
